﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reimers.Map;
using ProcolDemo.Geolocation;
using ProcolDemo.MyTeamsReference;

namespace ProcolDemo
{
    public partial class TeamLocation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Common.IsUserLoggedIn(Session))
                Response.Redirect("Default.aspx");

            Common.FillI18NStrings(this, Session);

            MyTeamsService myTeamsService = new MyTeamsService();
            String teamId = Request.QueryString["teamId"];
            MemberBean[] teamMembers = myTeamsService.GetTeamMembers(int.Parse(teamId));


            #region Location
            String lastIp = "189.140.213.217";
            var geoService = new GeolocationService();
            
            if (!String.IsNullOrEmpty(lastIp))
            {
                String[] location = geoService.GetGeoInfo(lastIp);
                Console.WriteLine(location);
                // Lat, Long
                double latitude;
                double longitude;

                if (Double.TryParse(location[0], out latitude) && Double.TryParse(location[1], out longitude))
                {
                    GMap.Latitude = latitude;
                    GMap.Longitude = longitude;
                    GMap.Zoom = 2;
                    GMap.Controls.Add(new Reimers.Map.Controls.GoogleSmallMapTypeControl("smc"));
                    GMap.Controls.Add(new Reimers.Map.Controls.GoogleSmallMapControl("sc"));

                    var icon1 = new GoogleIcon("green", "http://www.reimers.dk/images/icons/icong.png", "http://www.google.com/mapfiles/shadow50.png", new GoogleSize(20, 34), new GoogleSize(37, 34), new GooglePoint(9, 34), new GooglePoint(0, 0), new GooglePoint(9, 34));
                    GMap.Overlays.Clear();
                    for (int i = 0; i < teamMembers.Length; i++)
                    {
                        String[] locationMember = geoService.GetGeoInfo(teamMembers[i].lastIp);
                        Double.TryParse(locationMember[0], out latitude);
                        Double.TryParse(locationMember[1], out longitude);
                        GoogleLatLng p1 = new GoogleLatLng(latitude, longitude);
                        GoogleMarker m = new GoogleMarker(teamMembers[i].userId.ToString(), p1) { MarkerText = "X marks the spot", Options = new GoogleMarkerOptions(teamMembers[i].name +" "+ teamMembers[i].lastnameD, icon1) };
                        GMap.Icons.Add(icon1);
                        GMap.Overlays.Add(m);
                    }
                }
            }
            #endregion
        }
    }
}
